package com.yxh.www.author.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxh.www.author.domain.SmAccountApiRelation;
import com.yxh.www.author.mapper.SmAccountApiRelationMapper;
import com.yxh.www.author.service.SmAccountApiRelationService;
import com.yxh.www.common.result.Result;
import com.yxh.www.common.result.ResultBuilder;
import com.yxh.www.common.result.ResultEnum;
import com.yxh.www.mysql.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 系统账户接口关系 服务实现类
 * </p>
 *
 * @author Yangxiaohui
 * @since 2020-05-09
 */
@Service
public class SmAccountApiRelationServiceImpl extends BaseServiceImpl<SmAccountApiRelationMapper, SmAccountApiRelation> implements SmAccountApiRelationService {

    /**
     * 增加第三方账户接口关系
     *
     * @param accountId 第三方账户ID
     * @param apiIds    接口ID集合
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> addSmAccountApiRelation(String accountId, String[] apiIds) {
        if (apiIds.length==0){
            return ResultBuilder.error(ResultEnum.USER_HANDEL_ERROR,"无效的接口ID");
        }
        // 构建待插入集合
        List<SmAccountApiRelation> smAccountApiRelationList=new ArrayList<>();
        // 当前时间
        LocalDateTime nowTime=LocalDateTime.now();
        for (String apiId:apiIds){
            smAccountApiRelationList.add(new SmAccountApiRelation(accountId,apiId).setCreateTime(nowTime));
        }
        // 持久化数据
        baseMapper.insertList(smAccountApiRelationList);
        return ResultBuilder.success();
    }

    /**
     * 根据关系ID集合删除 账户接口关系
     *
     * @param ids 关系ID集合
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmAccountApiRelation(String[] ids) {
        baseMapper.deleteBatchIds(Arrays.asList(ids));
        return ResultBuilder.success();
    }

    /**
     * 根据第三方账户ID删除 账户接口关系
     *
     * @param accountId 第三方账户ID
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmAccountApiRelationByAccountId(String accountId) {
        baseMapper.delete(new QueryWrapper<SmAccountApiRelation>().eq("account_id",accountId));
        return ResultBuilder.success();
    }

    /**
     * 根据接口ID删除 账户接口关系
     *
     * @param apiId 接口ID
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmAccountApiRelationByApiId(String apiId) {
        baseMapper.delete(new QueryWrapper<SmAccountApiRelation>().eq("api_id",apiId));
        return ResultBuilder.success();
    }

    /**
     * 根据接口ID和账户ID删除 账户接口关系
     *
     * @param accountId 第三方账户ID
     * @param apiId     接口ID
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmAccountApiRelationByAccountIdAndApiId(String accountId, String apiId) {
        baseMapper.delete(new QueryWrapper<SmAccountApiRelation>()
                .eq("account_id",accountId)
                .eq("api_id",apiId)
        );
        return ResultBuilder.success();
    }
}
